package com.cloud.spring.demo.controller;

import com.cloud.spring.demo.entity.User;
import com.cloud.spring.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    User user;
    @Autowired
    UserService userService;

    @GetMapping("insert")
    public int insert() {
        user.setId(2);
        user.setName("tom");
        user.setCreateTime(LocalDateTime.now());
        user.setBirth(LocalDate.of(1993, 9, 16));
        return userService.insert(user);
    }

    @GetMapping("findById2")
    public List<User> findById2(int id) {
        return userService.findById2(id);
    }

    @GetMapping("findById3")
    public List<User> findById3(int id) {
        return userService.findById3(id);
    }

    @GetMapping("updateUserById")
    public int updateUserById() {
        user.setId(2);
        user.setName("jack");
        user.setCreateTime(LocalDateTime.now());
        user.setBirth(LocalDate.of(1994, 2, 13));
        return userService.updateUserById(user);
    }

    @GetMapping("insertBatch")
    public int insertBatch() {
        List<User> userList = new ArrayList<>();
        User user1 = new User();
        user1.setId(3);
        user1.setName("tom");
        user1.setCreateTime(LocalDateTime.now());
        user1.setBirth(LocalDate.of(1953, 9, 16));
        User user2 = new User();
        user2.setId(4);
        user2.setName("ter");
        user2.setCreateTime(LocalDateTime.now());
        user2.setBirth(LocalDate.of(1963, 9, 16));
        User user3 = new User();
        user3.setId(5);
        user3.setName("sony");
        user3.setCreateTime(LocalDateTime.now());
        user3.setBirth(LocalDate.of(1973, 9, 16));
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        return userService.insertBatch(userList);
    }

    @GetMapping("selectBatch")
    public List<User> selectBatch() {
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        ids.add(5);
        return userService.selectBatch(ids);
    }
}
